﻿using System.Text.Json;

namespace GuaHao
{
    public class TextJson
    {
        /// <summary>
        /// 读取用户Json
        /// </summary>
        /// <returns></returns>
        public static List<User> ReadUser(string path)
        {
            return JsonSerializer.Deserialize<List<User>>(File.ReadAllText(path)) ?? new List<User>();
        }
        /// <summary>
        /// 添加用户Json
        /// </summary>
        /// <param name="textjson"></param>
        /// <returns></returns>
        public static bool WriteUser(List<User> textjson, string path)
        {
            // 将List<Guahao> 转换为JSON字符串
            string jsonString = JsonSerializer.Serialize(textjson);
            // 将JSON字符串写入文件
            using (StreamWriter streamWriter = new StreamWriter(path, false))
            {
                streamWriter.Write(jsonString);
            }
            return true;
        }
        /// <summary>
        /// 读取患者信息Json
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static List<Patients> ReadPatients(string path)
        {
            return JsonSerializer.Deserialize<List<Patients>>(File.ReadAllText(path)) ?? new List<Patients>();
        }
        /// <summary>
        /// 添加患者信息Json
        /// </summary>
        /// <param name="textjson"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool WritePatients(List<Patients> textjson, string path)
        {
            // 将List<Patients> 转换为JSON字符串
            string jsonString = JsonSerializer.Serialize(textjson);
            // 将JSON字符串写入文件
            using (StreamWriter streamWriter = new StreamWriter(path, false))
            {
                streamWriter.Write(jsonString);
            }
            return true;
        }

        /// <summary>
        /// 读取科室信息Json
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static List<Department> ReadDepartment(string path)
        {
            return JsonSerializer.Deserialize<List<Department>>(File.ReadAllText(path)) ?? new List<Department>();
        }
        /// <summary>
        /// 添加科室信息Json
        /// </summary>
        /// <param name="textjson"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool WriteDepartment(List<Department> textjson, string path)
        {
            // 将List<Patients> 转换为JSON字符串
            string jsonString = JsonSerializer.Serialize(textjson);
            // 将JSON字符串写入文件
            using (StreamWriter streamWriter = new StreamWriter(path, false))
            {
                streamWriter.Write(jsonString);
            }
            return true;
        }
    }
}
